app.controller('shopBusinessEditCtrl',['$scope','$state','shopBusinessEditService','toaster',function($scope,$state,shopBusinessEditService,toaster){
    $scope.path=$scope.app.image.shopinfo;

    $scope.frontLimit=1;
    $scope.frontImages=new Array();
    $scope.frontImgQueue=new Array();

    $scope.backLimit=1;
    $scope.backImages=new Array();
    $scope.backQueue=new Array();

    $scope.userLimit=1;
    $scope.userImages=new Array();
    $scope.userQueue=new Array();


    $scope.subjectLimit=1;
    $scope.subjectImages=new Array();
    $scope.subjectQueue=new Array();


    $scope.shopInfo={
        identityType:0,
        subjectType:0
    };

    $scope.imgType=-1;

    $scope.delFrontImage=function(){
        $scope.imgType=0;
        $scope.delImg();
    }

    $scope.delBackImage=function(){
        $scope.imgType=1;
        $scope.delImg();
    }

    $scope.delUserImage=function(){
        $scope.imgType=2;
        $scope.delImg();
    }

    $scope.delSubjectImage= function () {
        $scope.imgType=3;
        $scope.delImg();
    }

    $scope.isDel=false;

    $scope.delImg=function(){
        if($scope.isDel){
            return;
        }

        $scope.isDel=true;

        shopBusinessEditService.delImage($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.isDel=false;
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findByShopId();
                    break;
                default :
                    $scope.isDel=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.init=function(){
        $scope.frontLimit=1;
        $scope.frontImages=new Array();

        $scope.backLimit=1;
        $scope.backImages=new Array();

        $scope.userLimit=1;
        $scope.userImages=new Array();


        $scope.subjectLimit=1;
        $scope.subjectImages=new Array();
    }

    $scope.findByShopId=function(){
        shopBusinessEditService.findByShopId($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.init();
                    $scope.shopInfo=result.data;

                    if($scope.shopInfo.identityBackImgUrl!=null&& !angular.isUndefined($scope.shopInfo.identityBackImgUrl)){
                        $scope.backImages.push($scope.shopInfo.identityBackImgUrl);
                        $scope.backLimit=0;
                    }

                    if($scope.shopInfo.identityFrontImgUrl!=null && !angular.isUndefined($scope.shopInfo.identityFrontImgUrl)){
                        $scope.frontImages.push($scope.shopInfo.identityFrontImgUrl);
                        $scope.frontLimit=0;
                    }

                    if($scope.shopInfo.identityImgUrl!=null && !angular.isUndefined($scope.shopInfo.identityImgUrl)){
                        $scope.userImages.push($scope.shopInfo.identityImgUrl);
                        $scope.userLimit=0;
                    }

                    if($scope.shopInfo.subjectImgUrl!=null && !angular.isUndefined($scope.shopInfo.subjectImgUrl)){
                        $scope.subjectImages.push($scope.shopInfo.subjectImgUrl);
                        $scope.subjectLimit=0;
                    }

                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.findByShopId();


    $scope.update = function () {

        if($scope.backQueue.length>0){
            $scope.shopInfo.identityBackImgUrl=$scope.backQueue[0].fileName;
        }else{
            delete $scope.shopInfo.identityBackImgUrl;
        }

        if($scope.frontImgQueue.length>0){
            $scope.shopInfo.identityFrontImgUrl=$scope.frontImgQueue[0].fileName;
        }else{
            delete $scope.shopInfo.identityFrontImgUrl;
        }

        if($scope.userQueue.length>0){
            $scope.shopInfo.identityImgUrl=$scope.userQueue[0].fileName;
        }else{
            delete $scope.shopInfo.identityImgUrl;
        }

        if($scope.subjectQueue.length>0){
            $scope.shopInfo.subjectImgUrl=$scope.subjectQueue[0].fileName;
        }else{
            delete $scope.shopInfo.subjectImgUrl;
        }

        delete $scope.shopInfo.businessTerm;

        shopBusinessEditService.update($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $state.go('app.shopDatum');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }


}])